package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.situ.web.dao.IBanjiDao;
import com.situ.web.entity.Banji;
import com.situ.web.entity.Course;
import com.situ.web.util.JDBCUtil;
import com.situ.web.util.ModelConvertUtil;

public class BanjiDaoImpl implements IBanjiDao {

	@Override
	public List<Banji> selectAll() {
		ArrayList<Banji> list = new ArrayList<Banji>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name` FROM banji";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}

			for (Banji banji : list) {
				System.out.println(banji);
			}
			// 6、关闭连接
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}

	@Override
	public List<Banji> selectByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Banji> search(String searchName) {
		ArrayList<Banji> list = new ArrayList<Banji>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,`name` from banji where name like ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, "%" + searchName + "%");
			System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String banjiName = resultSet.getString("name");
				Banji banji = new Banji(id, banjiName);
				list.add(banji);
			}

			for (Banji banji : list) {
				System.out.println(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}

		return list;
	}

	@Override
	public Integer insert(Banji banji) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO banji(`name`) VALUES(?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, banji.getName());
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public Integer update(Banji banji) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Update banji SET `name`=? WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, banji.getName());
			preparedStatement.setInt(2, banji.getId());
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("更新成功");
			} else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public Integer deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM banji WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;

	}

	@Override
	public Integer getTotalCount() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Banji> pageList(Integer offset, Integer pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Banji selectById(int id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Banji banji = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`FROM banji WHERE id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				int banjiId = resultSet.getInt("id");
				String name = resultSet.getString("name");
				banji = new Banji(banjiId, name);
			}
			// 6、关闭连接
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return banji;
	}

	@Override
	public List<Map<String, Object>> selectBanjiCourseInfo() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		List<Map<String, Object>> list = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT b.id,b.name,c.name AS courseName,c.credit\r\n" + 
					"FROM banji AS b INNER JOIN banji_course AS bc \r\n" + 
					"ON b.id=bc.banji_id\r\n" + 
					"INNER JOIN course AS c\r\n" + 
					"ON bc.course_id=c.id;";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			list = ModelConvertUtil.converToList(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public List<Course> selectCourse() {
		ArrayList<Course> list = new ArrayList<Course>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,`name`,credit FROM course";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int credit = resultSet.getInt("credit");
				Course course = new Course(id, name,credit);
				list.add(course);
			}

			for (Course course : list) {
				System.out.println(course);
			}
			// 6、关闭连接
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	}

	@Override
	public int chooseCourse(int classId, int courseId) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO banji_course(banji_id,course_id) VALUES(?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, classId);
			preparedStatement.setInt(2, courseId);
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("选课成功");
			} else {
				System.out.println("选课失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

}
